package com.caipin.task;

import com.caipin.domain.DetectingCode;
import com.caipin.domain.Mobile;
import com.caipin.domain.QiDianDetectionCodeData;
import com.caipin.domain.RegisterMessage;
import com.caipin.service.MobileService;
import com.caipin.utils.ImsiConvertUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2020-04-03 11:15
 * @Description: TODO
 */
@Component
@Slf4j
public class AsyncTask {

    @Autowired
    private MobileService mobileService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @Async
    public void postDetectionCodeDataToKafka(List<DetectingCode> detectingCodeList, RegisterMessage registerMessage) {

        for (DetectingCode detectingCode : detectingCodeList) {

            detectingCode.setServiceProviderNumber(ImsiConvertUtils.toServiceProviderNumber(detectingCode.getImsi()));
            detectingCode.setPhoneSeven(ImsiConvertUtils.toMobilePhoneNumberHead(detectingCode.getImsi()));
            detectingCode.setNetWorkTypeNumber(ImsiConvertUtils.toNetWorkTypeNumber(detectingCode.getCellNumber()));

            boolean mobileFlag = false;
            Mobile mobile = mobileService.findByMobile(detectingCode.getPhoneSeven());
            if (mobile != null) {
                mobileFlag = true;
            }


            QiDianDetectionCodeData qiDianDetectionCodeData = new QiDianDetectionCodeData()
                    .setRecord_id("90d31c74-5644-4645-a727-7de682912dcf")
                    .setSite_name(registerMessage.getDevAddresss())
                    .setSite_sn("000020")
                    .setDevice_sn(registerMessage.getDevNumber())
                    .setImsi(detectingCode.getImsi())
                    .setImei(detectingCode.getImei())
                    .setStmsi("")
                    .setMac(detectingCode.getMac())
                    .setMsisdn(detectingCode.getPhoneSeven())
                    .setCapture_time(detectingCode.getUptime() * 1000)
                    .setCity_code(mobileFlag == true ? mobile.getAdminCode() : "")
                    .setCity_name(mobileFlag == true ? mobile.getProvince() + mobile.getCity() : "")
                    .setBand(ImsiConvertUtils.toBand(detectingCode.getCellNumber()))
                    .setNetwork(detectingCode.getNetWorkTypeNumber())
                    .setSpcode(detectingCode.getServiceProviderNumber())
                    .setLongitude(registerMessage.getDevPos().getLongitude())
                    .setLatitude(registerMessage.getDevPos().getLatitude())
                    .setManufacturer("空军第六研究所")
                    .setSave_time(System.currentTimeMillis());

            try {
                String jsonStr = objectMapper.writeValueAsString(qiDianDetectionCodeData);
                log.info(jsonStr);

                kafkaTemplate.send("data_elec_fence", jsonStr);


            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }


        }
    }
}
